

from typing import Set, Tuple

from datetime import datetime
from enum import Enum

from prettytable import PrettyTable

from colorama import init as colorama_init
from colorama import Fore, Back

import pandas as pd
import numpy as np


class MyPrettyTable:
    """"""
    def __init__(self):

        header = []
        header.append("日期")
        header.append(Column.SECONDARY_RALLY.value)
        header.append(Column.NATURAL_RALLY.value)
        header.append(Column.UPWARD_TREND.value)
        header.append(Column.DOWNWARD_TREND.value)
        header.append(Column.NATURAL_REACTION.value)
        header.append(Column.SECONDARY_REACTION.value)

        self.tb = PrettyTable(header)

    def add_fore_white(self, _str):
        """"""
        return Fore.WHITE + str(_str) + Fore.RESET
    
    def add_fore_black(self, _str):
        """"""
        return Fore.BLACK+ str(_str) + Fore.RESET
    
    def add_fore_red(self, _str):
        """"""
        return Fore.RED + str(_str) + Fore.RESET

    def add_back_black(self, _str):
        """"""
        return Back.BLACK + str(_str) + Back.RESET
    
    def add_back_lightredex(self, _str):
        """"""
        return Back.LIGHTRED_EX + str(_str) + Back.RESET

    def show(self, df):
        """"""

        self.tb.clear_rows()

        for idx, row in df.iterrows():

            c1 = row[Column.SECONDARY_RALLY]
            c2 = row[Column.NATURAL_RALLY]
            c3 = row[Column.UPWARD_TREND]
            c4 = row[Column.DOWNWARD_TREND]
            c5 = row[Column.NATURAL_REACTION]
            c6 = row[Column.SECONDARY_REACTION]
            c7 = row["isPivotal"]

            if np.isnan(c1):
                _c1 = ''
            else:
                _c1 = self.add_fore_white(c1)

            if np.isnan(c2):
                _c2 = ''
            else:
                _c2 = self.add_fore_white(c2)
                if c7 == True:
                    _c2 = self.add_back_black(_c2)

            if np.isnan(c3):
                _c3 = ''
            else:
                _c3 = self.add_fore_black(c3)
                if c7 == True:
                    _c3 = self.add_back_lightredex(_c3)
            
            if np.isnan(c4):
                _c4 = ''
            else:
                _c4 = self.add_fore_red(c4)
                if c7 == True:
                    _c4 = self.add_back_black(_c4)

            if np.isnan(c5):
                _c5 = ''
            else:
                _c5 = self.add_fore_white(c5)
                if c7 == True:
                    _c5 = self.add_back_lightredex(_c5)

            if np.isnan(c6):
                _c6 = ''
            else:
                _c6 = self.add_fore_white(c6)

            _row = [idx, _c1, _c2, _c3, _c4, _c5, _c6]
            
            self.tb.add_row(_row)
        
        print(self.tb)


class Column(Enum):
    """
    记录栏
    """
    SECONDARY_RALLY = "次级回升"
    NATURAL_RALLY = "自然回升"
    UPWARD_TREND = "上升趋势"
    DOWNWARD_TREND = "下降趋势"
    NATURAL_REACTION = "自然回撤"
    SECONDARY_REACTION = "次级回撤"


class PriceAction:

    def __init__(self, three_break = 6, six_pivot = 12, date_range_freq = "1min"):
        """"""

        """
        pivotal_prices 形如
        {
            Column.UPWARD_TREND: [(dt1, price1), (dt2, price2), ..., (dtn, pricen)],
            Column.DOWNWARD_TREND: [(dt1, price1), (dt2, price2), ..., (dtn, pricen)],
            Column.NATURAL_REACTION: [(dt1, price1), (dt2, price2), ..., (dtn, pricen)],
            Column.NATURAL_RALLY: [(dt1, price1), (dt2, price2), ..., (dtn, pricen)],
        }
        """

        self.three_break = three_break
        self.six_pivot = six_pivot

        self.udpate_row = False
        self.rows_pre_page = 36 # 代表1个page。
        self.page = None

        self.date_range_freq = date_range_freq

        self.df = None
        self.pivotal_prices = {}

        self.inited = False

        self.main_trend = Column.UPWARD_TREND
        self.cur_column = Column.UPWARD_TREND

        self.tb = MyPrettyTable()

        colorama_init()

    def init_df(self, price, dt):
        """"""
        column_names = []
        # column_names.append("datetime")
        column_names.append(Column.SECONDARY_RALLY)
        column_names.append(Column.NATURAL_RALLY)
        column_names.append(Column.UPWARD_TREND)
        column_names.append(Column.DOWNWARD_TREND)
        column_names.append(Column.NATURAL_REACTION)
        column_names.append(Column.SECONDARY_REACTION)

        column_names.append("isPivotal")

        # 创建一个rows_pre_page大小的empyt dataframe。
        # https://pandas.pydata.org/docs/reference/api/pandas.date_range.html
        # Return a fixed frequency DatetimeIndex.
        index = pd.date_range(start = dt, periods=1, freq=self.date_range_freq)

        self.df = pd.DataFrame(index=index, columns=column_names)

        # 把第一行全部赋值为price，因为在update的逻辑中比较的数值大小。
        self.df.loc[dt] = price
        # self.df.loc["isPivotal"][0] = False
        self.df.loc[dt, "isPivotal"] = False

        self.page = pd.DataFrame(index=index, columns=column_names)

    def mark_pivotal_price(self, main_trend, cur_column, prev_column):
        """
        标记关键点。
        """
        is_pivotal_price = False

        # rule: 4a
        if cur_column == Column.NATURAL_REACTION and \
            prev_column == Column.UPWARD_TREND:
            is_pivotal_price = True
        # rule: 4b
        elif main_trend == Column.UPWARD_TREND and \
            cur_column in [Column.NATURAL_RALLY, Column.UPWARD_TREND] and \
            prev_column == Column.NATURAL_REACTION:
            is_pivotal_price = True
        # rule: 4c
        elif cur_column == Column.NATURAL_RALLY and \
            prev_column == Column.DOWNWARD_TREND:
            is_pivotal_price = True
        # rule: 4d
        elif main_trend == Column.DOWNWARD_TREND and \
            cur_column in [Column.NATURAL_REACTION, Column.DOWNWARD_TREND] and \
            prev_column == Column.NATURAL_RALLY:
            is_pivotal_price = True
        
        if is_pivotal_price:
            index = self.df[prev_column].last_valid_index()
            # self.df['isPivotal'].loc[index] = True
            self.df.loc[index, 'isPivotal'] = True

            self.update_pivotal_prices(prev_column, index)

    def update_pivotal_prices(self, prev_column, index):
        """"""
        # price = self.df[prev_column].loc[index]
        price = self.df.at[index, prev_column]

        lst = self.pivotal_prices.get(prev_column, [])
        lst.append((index, price))

        self.pivotal_prices.update({prev_column: lst}) 

    def set_price(self, dt, price, column):
        """
        给df中当前列(self.cur_column)指定的dt(最新？)赋值。
        """
        if column is not None:
            self.df.at[dt, column] = price

        # else:
        #     self.df.loc[dt] = np.nan

    def next_record_column(self, price, cur_column) -> Column:
        """
        价格没有变化则返回None
        """
        # ------ 3点突破，转换记录栏 ------ #
        # 5a 这里需要判断主趋势。
        if cur_column == Column.NATURAL_RALLY and self.main_trend == Column.DOWNWARD_TREND:
            lst = self.pivotal_prices.get(Column.NATURAL_RALLY, [])
            if lst and price - lst[-1][1] > self.three_break:
                return Column.UPWARD_TREND
        # 5b 这里需要判断主趋势。
        if cur_column == Column.NATURAL_REACTION and self.main_trend == Column.UPWARD_TREND:
            lst = self.pivotal_prices.get(Column.NATURAL_REACTION, [])
            if lst and price - lst[-1][1] < -self.three_break:
                return Column.DOWNWARD_TREND


        # ------ 6点转向，转换记录栏 ------ #
        s1 = self.df[Column.SECONDARY_RALLY]
        s2 = self.df[Column.NATURAL_RALLY]
        s3 = self.df[Column.UPWARD_TREND]
        s4 = self.df[Column.DOWNWARD_TREND]
        s5 = self.df[Column.NATURAL_REACTION]
        s6 = self.df[Column.SECONDARY_REACTION]


        s1_v = s1.loc[s1.last_valid_index()]
        s2_v = s2.loc[s2.last_valid_index()]
        s3_v = s3.loc[s3.last_valid_index()]
        s4_v = s4.loc[s4.last_valid_index()]
        s5_v = s5.loc[s5.last_valid_index()]
        s6_v = s6.loc[s6.last_valid_index()]
        
        # 上升趋势未来的变化形态有两种：
        # 1，自然回撤
        # 2，上升趋势（继续）
        if cur_column == Column.UPWARD_TREND:
            # rule: 6a
            if price - s3_v <= -self.six_pivot:
                return Column.NATURAL_REACTION
            # rule: 6d    
            elif price > s3_v:
                return Column.UPWARD_TREND


        # 下降趋势未来有两种变化：
        # 1，自然回升
        # 2，下降趋势（继续）
        if cur_column == Column.DOWNWARD_TREND:  
            # rule: 6c
            if price - s4_v >= self.six_pivot:
                return Column.NATURAL_RALLY
            # rule: 6b    
            elif price < s4_v:
                return Column.DOWNWARD_TREND

        # 自然回升未来有四种变化：
        # 1，次级回撤
        # 2，自然回撤
        # 4，上升趋势
        # 5，自然回升（继续）
        if cur_column == Column.NATURAL_RALLY:
            # rule: 6f
            if price > s3_v:# and self.main_trend == Column.UPWARD_TREND:
                return Column.UPWARD_TREND
            elif price > s2_v:
                return Column.NATURAL_RALLY
            elif price - s2_v <= -self.six_pivot:
                if price > s5_v:
                    # rule: 6h
                    return Column.SECONDARY_REACTION
                elif price < s5_v:
                    return Column.NATURAL_REACTION
  

        # 自然回撤未来有四种变化：
        # 1，次级回升
        # 2，自然回升
        # 4，下降趋势
        # 5，自然回撤
        if cur_column == Column.NATURAL_REACTION:
            # rule: 6e
            if price < s4_v:# and self.main_trend == Column.DOWNWARD_TREND:
                return Column.DOWNWARD_TREND
            elif price < s5_v:
                return Column.NATURAL_REACTION
            elif price - s5_v >= self.six_pivot:
                # rule: 6g
                if price < s2_v:
                    return Column.SECONDARY_RALLY
                elif price > s2_v:
                    return Column.NATURAL_RALLY


        # 次级回升未来有五种变化：
        # 1，次级回撤
        # 2，自然回撤
        # 4，自然回升
        # 5，上升趋势
        # 6，次级回升（继续）
        if cur_column == Column.SECONDARY_RALLY:
            if price > s3_v and self.main_trend == Column.UPWARD_TREND:
                return Column.UPWARD_TREND
            elif price > s2_v:
                return Column.NATURAL_RALLY
            elif price > s1_v:
                return Column.SECONDARY_RALLY
            elif price - s1_v <= -self.six_pivot:
                if price > s5_v:
                    return Column.SECONDARY_REACTION
                elif price < s5_v:
                    return Column.NATURAL_REACTION

        # 次级回撤未来有五种变化：
        # 1，次级回升
        # 2，自然回升
        # 4，自然回撤
        # 5，下降趋势
        # 6，次级回撤（继续）
        if cur_column == Column.SECONDARY_REACTION:
            if price < s4_v and self.main_trend == Column.DOWNWARD_TREND:
                return Column.DOWNWARD_TREND
            elif price < s5_v:
                return Column.NATURAL_REACTION
            elif price < s6_v:
                return Column.SECONDARY_REACTION
            elif price - s6_v >= self.six_pivot:
                if price < s2_v:
                    return Column.SECONDARY_RALLY
                elif price > s2_v:
                    return Column.NATURAL_RALLY

        return None

    def update(self, price: float, dt: datetime):
        """"""
        # dt = dt.strftime("%Y-%m-%d %H:%M:%S")
        # dt = dt.replace(second=0, microsecond=0)

        if not self.inited:
            self.init_df(price, dt)
            self.inited = True
            return
        
        self.udpate_row = False

        next_column = self.next_record_column(price, self.cur_column)

        if next_column is not None:

            self.set_price(dt, price, next_column)

            self.udpate_row = True

            # 标记关键点。
            if next_column != self.cur_column:
                self.mark_pivotal_price(self.main_trend, next_column,  self.cur_column)

            # 更新当前主趋势
            if next_column in [Column.UPWARD_TREND, Column.DOWNWARD_TREND]:
                self.main_trend = next_column

            self.cur_column = next_column

    def create_pivots_df(self):
        """"""
        index = pd.date_range(start =("1970-01-01 00:00:00"), periods=6, freq=self.date_range_freq)
        pivots_df = pd.DataFrame(index=index, columns=self.df.columns)

        s1 = self.df[Column.SECONDARY_RALLY]
        s2 = self.df[Column.NATURAL_RALLY]
        s3 = self.df[Column.UPWARD_TREND]
        s4 = self.df[Column.DOWNWARD_TREND]
        s5 = self.df[Column.NATURAL_REACTION]
        s6 = self.df[Column.SECONDARY_REACTION]    
        s7 = self.df["isPivotal"]

        s1_index = s1.last_valid_index()
        s2_index = s2.last_valid_index()
        s3_index = s3.last_valid_index()
        s4_index = s4.last_valid_index()
        s5_index = s5.last_valid_index()
        s6_index = s6.last_valid_index()
        s7_index = s7.last_valid_index()

        latest_datetime = max(s2_index, s3_index, s4_index, s5_index)

        if latest_datetime == s3_index:
            # print("上升趋势上一次价格",s3.loc[s3_index])
            pivots_df.iloc[0, 2] = s3.loc[s3_index]
            pivots_df.iloc[0, 6] = s7.loc[s3_index]

        elif latest_datetime == s4_index:
            # print("下降趋势上一次价格",s4.loc[s4_index])
            pivots_df.iloc[0, 3] = s4.loc[s4_index]
            pivots_df.iloc[0, 6] = s7.loc[s4_index]

        elif latest_datetime == s2_index:
            if s3_index > s4_index:
                # 当前为上升趋势
                # print("自然回升上一次价格", s2.loc[s2_index], s2_index)
                # print("自然回撤上一次价格", s5.loc[s5_index], s5_index)
                # print("上升趋势上一次价格", s3.loc[s3_index], s3_index)

                pivots_df.iloc[0, 2] = s3.loc[s3_index]
                pivots_df.iloc[1, 4] = s5.loc[s5_index]
                pivots_df.iloc[2, 1] = s2.loc[s2_index]

                pivots_df.iloc[0, 6] = s7.loc[s3_index] 
                pivots_df.iloc[1, 6] = s7.loc[s5_index] 
                pivots_df.iloc[2, 6] = s7.loc[s2_index]  

            elif s3_index < s4_index:
                # 当前为下降趋势
                # print("自然回升上一次价格", s2.loc[s2_index], s2_index)
                # print("下降趋势上一次价格", s4.loc[s4_index], s4_index)

                pivots_df.iloc[0, 3] = s4.loc[s4_index]
                pivots_df.iloc[1, 1] = s2.loc[s2_index]

                pivots_df.iloc[0, 6] = s7.loc[s4_index]
                pivots_df.iloc[1, 6] = s7.loc[s2_index]

        elif latest_datetime == s5_index:
            if s4_index > s3_index:
                # 当前为下降趋势
                # print("自然回撤上一次价格", s5.loc[s5_index], s5_index)
                # print("自然回升上一次价格", s2.loc[s2_index], s2_index)
                # print("下降趋势上一次价格", s4.loc[s4_index], s4_index)

                pivots_df.iloc[0, 3] = s4.loc[s4_index]
                pivots_df.iloc[1, 1] = s2.loc[s2_index]
                pivots_df.iloc[2, 4] = s5.loc[s5_index]

                pivots_df.iloc[0, 6] = s7.loc[s4_index]
                pivots_df.iloc[1, 6] = s7.loc[s2_index] 
                pivots_df.iloc[2, 6] = s7.loc[s5_index]

            elif s4_index < s3_index:
                # 当前为上升趋势
                # print("自然回撤上一次价格", s5.loc[s5_index], s5_index)
                # print("上升趋势上一次价格", s3.loc[s3_index], s3_index)

                pivots_df.iloc[0, 2] = s3.loc[s3_index]
                pivots_df.iloc[1, 4] = s5.loc[s5_index]

                pivots_df.iloc[0, 6] = s7.loc[s3_index]
                pivots_df.iloc[1, 6] = s7.loc[s5_index]

        return pivots_df
    
    def get_pivotals(self) -> Tuple:
        """
        获取最右侧关键点位。
        比如：上升趋势，获取上升关键点，自然回撤关键点。
             下降趋势，获取下降关键点，自然回升关键点。
        """

        # s2 = self.df[Column.NATURAL_RALLY]
        s3 = self.df[Column.UPWARD_TREND]
        s4 = self.df[Column.DOWNWARD_TREND]
        # s5 = self.df[Column.NATURAL_REACTION]

        # s2_index = s2.last_valid_index()
        s3_index = s3.last_valid_index()
        s4_index = s4.last_valid_index()
        # s5_index = s5.last_valid_index()

        upward_pivots = self.pivotal_prices.get(Column.UPWARD_TREND)
        downward_pivots = self.pivotal_prices.get(Column.DOWNWARD_TREND)
    
        natural_rally_pivots = self.pivotal_prices.get(Column.NATURAL_RALLY)
        natural_reaction_pivots = self.pivotal_prices.get(Column.NATURAL_REACTION)

        upward_pivot = None
        downward_pivot = None
        natural_rally_pivot = None
        natural_reaction_pivot = None

        if self.main_trend == Column.UPWARD_TREND:

            assert s3_index > s4_index
            
            if upward_pivots:
                upward_pivot = upward_pivots[-1][1]

            if natural_reaction_pivots and natural_reaction_pivots[-1][0] > s3_index:
                natural_reaction_pivot = natural_reaction_pivots[-1][1]

            return (self.main_trend, upward_pivot, natural_reaction_pivot)

        elif self.main_trend == Column.DOWNWARD_TREND:

            assert s4_index > s3_index

            if downward_pivots:
                downward_pivot = downward_pivots[-1][1]

            if natural_rally_pivots and natural_rally_pivots[-1][0] > s4_index:
                natural_rally_pivot = natural_rally_pivots[-1][1]
            
            return (self.main_trend, downward_pivot, natural_rally_pivot)
        
        return (None, None, None)

    def show(self, show_pivot = False):
        """"""
        if self.udpate_row:

            latest = self.df.iloc[-1]

            self.page = self.page.append(latest)

        if len(self.page.index) >= self.rows_pre_page:

            self.tb.show(self.page)

            self.tb.show(self.create_pivots_df())

            self.page = self.page[0:0]


        if show_pivot:

            pivotals = self.get_pivotals()
            main_trend = pivotals[0]

            if main_trend == Column.UPWARD_TREND:

                upward_pivot = pivotals[1]
                natural_reaction_pivot = pivotals[2]

                print(f"上升趋势关键点{upward_pivot}, 自然回撤关键点{natural_reaction_pivot}")

            elif main_trend == Column.DOWNWARD_TREND:

                downward_pivot = pivotals[1]
                natural_rally_pivot = pivotals[2]

                print(f"下降趋势关键点{downward_pivot}, 自然回升关键点{natural_rally_pivot}")

    def show_remaining(self):
        """
        静态数据分析的时候调用
        """
        if len(self.page.index) < self.rows_pre_page:
            self.tb.show(self.page)
            self.tb.show(self.create_pivots_df())


class PriceActionPct(PriceAction):
    
    def __init__(self, three_break_pct = 0.03, six_pivot_pct = 0.06, date_range_freq="1min"):
        

        self.three_break_pct = three_break_pct
        self.six_pivot_pct = six_pivot_pct

        self.udpate_row = False
        self.rows_pre_page = 36 # 代表1个page。
        self.page = None

        self.date_range_freq = date_range_freq

        self.df = None
        self.pivotal_prices = {}

        self.inited = False

        self.main_trend = Column.UPWARD_TREND
        self.cur_column = Column.UPWARD_TREND

        self.tb = MyPrettyTable()

        colorama_init()

    def next_record_column(self, price, cur_column) -> Column:
        """
        价格没有变化则返回None
        """
        # ------ 3点突破，转换记录栏 ------ #
        # 5a 这里需要判断主趋势。
        if cur_column == Column.NATURAL_RALLY and self.main_trend == Column.DOWNWARD_TREND:
            lst = self.pivotal_prices.get(Column.NATURAL_RALLY, [])
            if lst and (price - lst[-1][1])/lst[-1][1] > self.three_break_pct:
                return Column.UPWARD_TREND
        # 5b 这里需要判断主趋势。
        if cur_column == Column.NATURAL_REACTION and self.main_trend == Column.UPWARD_TREND:
            lst = self.pivotal_prices.get(Column.NATURAL_REACTION, [])
            if lst and (price - lst[-1][1])/lst[-1][1] < -self.three_break_pct:
                return Column.DOWNWARD_TREND


        # ------ 6点转向，转换记录栏 ------ #
        s1 = self.df[Column.SECONDARY_RALLY]
        s2 = self.df[Column.NATURAL_RALLY]
        s3 = self.df[Column.UPWARD_TREND]
        s4 = self.df[Column.DOWNWARD_TREND]
        s5 = self.df[Column.NATURAL_REACTION]
        s6 = self.df[Column.SECONDARY_REACTION]


        s1_v = s1.loc[s1.last_valid_index()]
        s2_v = s2.loc[s2.last_valid_index()]
        s3_v = s3.loc[s3.last_valid_index()]
        s4_v = s4.loc[s4.last_valid_index()]
        s5_v = s5.loc[s5.last_valid_index()]
        s6_v = s6.loc[s6.last_valid_index()]
        
        # 上升趋势未来的变化形态有两种：
        # 1，自然回撤
        # 2，上升趋势（继续）
        if cur_column == Column.UPWARD_TREND:
            # rule: 6a
            if s3_v and (price - s3_v)/s3_v <= -self.six_pivot_pct:
                return Column.NATURAL_REACTION
            # rule: 6d    
            elif price > s3_v:
                return Column.UPWARD_TREND


        # 下降趋势未来有两种变化：
        # 1，自然回升
        # 2，下降趋势（继续）
        if cur_column == Column.DOWNWARD_TREND:  
            # rule: 6c
            if s4_v and (price - s4_v)/s4_v >= self.six_pivot_pct:
                return Column.NATURAL_RALLY
            # rule: 6b    
            elif price < s4_v:
                return Column.DOWNWARD_TREND

        # 自然回升未来有四种变化：
        # 1，次级回撤
        # 2，自然回撤
        # 4，上升趋势
        # 5，自然回升（继续）
        if cur_column == Column.NATURAL_RALLY:
            # rule: 6f
            if price > s3_v:# and self.main_trend == Column.UPWARD_TREND:
                return Column.UPWARD_TREND
            elif price > s2_v:
                return Column.NATURAL_RALLY
            elif s2_v and (price - s2_v)/s2_v <= -self.six_pivot_pct:
                if price > s5_v:
                    # rule: 6h
                    return Column.SECONDARY_REACTION
                elif price < s5_v:
                    return Column.NATURAL_REACTION
  

        # 自然回撤未来有四种变化：
        # 1，次级回升
        # 2，自然回升
        # 4，下降趋势
        # 5，自然回撤
        if cur_column == Column.NATURAL_REACTION:
            # rule: 6e
            if price < s4_v:# and self.main_trend == Column.DOWNWARD_TREND:
                return Column.DOWNWARD_TREND
            elif price < s5_v:
                return Column.NATURAL_REACTION
            elif s5_v and (price - s5_v)/s5_v >= self.six_pivot_pct:
                # rule: 6g
                if price < s2_v:
                    return Column.SECONDARY_RALLY
                elif price > s2_v:
                    return Column.NATURAL_RALLY


        # 次级回升未来有五种变化：
        # 1，次级回撤
        # 2，自然回撤
        # 4，自然回升
        # 5，上升趋势
        # 6，次级回升（继续）
        if cur_column == Column.SECONDARY_RALLY:
            if price > s3_v and self.main_trend == Column.UPWARD_TREND:
                return Column.UPWARD_TREND
            elif price > s2_v:
                return Column.NATURAL_RALLY
            elif price > s1_v:
                return Column.SECONDARY_RALLY
            elif s1_v and (price - s1_v)/s1_v <= -self.six_pivot_pct:
                if price > s5_v:
                    return Column.SECONDARY_REACTION
                elif price < s5_v:
                    return Column.NATURAL_REACTION

        # 次级回撤未来有五种变化：
        # 1，次级回升
        # 2，自然回升
        # 4，自然回撤
        # 5，下降趋势
        # 6，次级回撤（继续）
        if cur_column == Column.SECONDARY_REACTION:
            if price < s4_v and self.main_trend == Column.DOWNWARD_TREND:
                return Column.DOWNWARD_TREND
            elif price < s5_v:
                return Column.NATURAL_REACTION
            elif price < s6_v:
                return Column.SECONDARY_REACTION
            elif s6_v and (price - s6_v)/s6_v >= self.six_pivot_pct:
                if price < s2_v:
                    return Column.SECONDARY_RALLY
                elif price > s2_v:
                    return Column.NATURAL_RALLY

        return None
        